﻿#include "FindDialog.h"
#include<QDebug>
#include<QTextCursor>
#include<QMessageBox>
FindDialog::FindDialog(QWidget * parent,QPlainTextEdit * mplain)
    :QDialog(parent,Qt::WindowCloseButtonHint| Qt::Drawer)
    ,m_plainText(mplain)
    /*,m_findlabel(this),m_findBtn(this),m_closeBtn(this),
                  m_matchBox(this),m_backBtn(this),m_frowardBtn(this),
                  m_groutBox(this),m_hbLayout(this),m_layout(this)*/
{
    qDebug()<<__FUNCSIG__;;
    m_findlabel.setText("find content:");
    m_findBtn.setText("Find Next");
    m_closeBtn.setText("Close");
    m_matchBox.setText("Match Case");
    m_backBtn.setText("Back Ward");
    m_frowardBtn.setText("Forward");
    m_groutBox.setTitle("Direction");

    qDebug()<<__LINE__<<__FUNCSIG__;

    m_frowardBtn.setChecked(true); // 设置默认是选上的

    m_hbLayout.addWidget(&m_frowardBtn);
    m_hbLayout.addWidget(&m_backBtn);

    m_groutBox.setLayout(&m_hbLayout);

    m_layout.setSpacing(10);
    qDebug()<<__LINE__<<__FUNCSIG__;

    m_layout.addWidget(&m_findlabel,0,0);

    m_layout.addWidget(&m_findEdit,0,1);
    m_layout.addWidget(&m_findBtn,0,2);
    m_layout.addWidget(&m_matchBox,1,0);
    m_layout.addWidget(&m_groutBox,1,1);
    m_layout.addWidget(&m_closeBtn,1,2);

    qDebug()<<__LINE__<<__FUNCSIG__;

    // 将布局设置到当前的查找对话框中去
    setLayout(&m_layout);

    // 设置当前的windTiTle
    setWindowTitle("find");
    qDebug()<<__LINE__<<__FUNCSIG__;

    setConnect();



}

bool FindDialog::event(QEvent *event)
{
    qDebug()<<__LINE__<<"  "<<__FUNCSIG__<<sender();
    if(event->type() == QEvent::Close)      // 如果事件类型是 关闭的事件
    {
        hide();                             // 隐藏对话框
        return true;                        // 返回 true

    }
    else{                                 //如果事件不是对话框的关闭
        QDialog::event(event);               //一定要是调用父组件的QDiaLog  event() 函数
    }                                       //这里是千万不能不能在调用 FindDialog::的event()

}

QString FindDialog::getData()
{
    return m_findEdit.text();               //返回 m_findEdit的数据
}

void FindDialog::setConnect()
{
    connect(&m_closeBtn,SIGNAL(clicked(bool)),this,SLOT(close()));   //
    connect(&m_findBtn,SIGNAL(clicked(bool)),this,SLOT(done()));
    connect(&m_findBtn,SIGNAL(clicked(bool)),this,SLOT(onSearch()));
}

void FindDialog::done()
{
    qDebug()<<__LINE__<<__FUNCSIG__;
    qDebug()<<getData();

}

void FindDialog::onSearch()
{
    static int lastIndex = 0 ;
    qDebug()<<__FUNCSIG__;
    QString target = m_findEdit.text();
    QString str_text=m_plainText->toPlainText();

    //qDebug()<<str_text;
    if(m_plainText && target!="")
    {
        QTextCursor cursor=m_plainText->textCursor();
        int index =-1;

        if(m_frowardBtn.isChecked())
        {
            index=str_text.indexOf(target,cursor.position(),m_matchBox.isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive);
            if(index!=-1)
            {
             cursor.setPosition(index);
             cursor.setPosition(index+ target.length(),QTextCursor::KeepAnchor);
             m_plainText->setTextCursor(cursor);
            }
        }

        if(m_backBtn.isChecked())
        {
            index=str_text.indexOf(target,cursor.position() - str_text.length() -1,m_matchBox.isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive);
            if(index!=-1)
            {
             cursor.setPosition(index);
             cursor.setPosition(index+ target.length(),QTextCursor::KeepAnchor);
             m_plainText->setTextCursor(cursor);
            }
        }

        if(index<0)
        {
            QMessageBox msg(this);
            msg.setText("not find the word "+ target + " ");
            msg.setWindowTitle("check");
            msg.setIcon(QMessageBox::Information);
            msg.setStandardButtons(QMessageBox::Ok);

            msg.exec();
        }

    }


}

void FindDialog::onClose()
{
    qDebug()<<__FUNCDNAME__;
    close();
}

bool FindDialog::eventFilter(QObject *obj, QEvent *e)
{
    bool ret = true;
    return ret;
}
